/*
# Miahx© 框架消息 FastDDS API 的 KANZI 支持类（C++端）测试文件

Version: 0.4.10,
Last updated: October 17, 2024

## Contributor(s)
* YAO Naiming <[naiming@iscas.ac.cn](mailto:naiming@iscas.ac.cn)>

## Change log
### v0.4.10
- .17: [更新] 新增对 PointCloudStreamReader 类型的测试
- .15: [新增] 迁移实现，修复 get_stream_handler 实现缺陷，新增两种调用方法测试

---
Copyright © 2024-2025 MIAHX Contributors, ISCAS, and ASIC. All Rights Reserved.
Licensed under Apache-2.0.
*/

#include "../src/miahx_fastdds_api/include/miahx/bng_classical_reader.h"
#include "../src/miahx_fastdds_api/include/miahx/point_cloud_reader.h"


//void on_data_available(void *ptr) {
//    auto frame = (BngClassicalDataFrame *) ptr;
//    auto hdr = frame->header();
//    auto &domain = hdr.header().domain();
//    auto &source = hdr.header().source();
//    cout << hdr.header().source() << endl;
//
//    auto &samples = frame->samples();
//    for (auto iter = samples.begin(); iter != samples.end(); iter++) {
//        auto &pos = iter->state_pos();
//        cout << "state_pos = [" << pos[0] << "," << pos[1] << "," << pos[2] << "]" << endl;
//    }
//}

void on_data_available(void *ptr) {
    auto frame = (PointCloudDataFrame *) ptr;
    auto hdr = frame->header();
    auto &domain = hdr.header().domain();
    auto &source = hdr.header().source();
    cout << hdr.header().source() << endl;

    auto &samples = frame->samples();
    for (auto & sample : samples) {
        auto points = sample.points();
        cout << "len(points) [" << points.size() << "]" << endl;
    }
}

void thread_func(unique_lock<mutex> &lock, condition_variable &cond) {
    this_thread::sleep_for(chrono::seconds(5));
    cond.notify_all();
}

int main() {
    mutex mtx;
    unique_lock<mutex> lock(mtx);
    condition_variable cond;
    thread th(thread_func, ref(lock), ref(cond));
    th.detach();

    Participant participant;

//    data_listener listener(on_data_available);
//    BngClassicalStreamReader reader(&participant, listener, nullptr);

    data_listener listener(on_data_available);
    PointCloudStreamReader reader(&participant, TOPIC_CAR_TRAJECTORY_PC, listener, nullptr);

    reader.wait(lock, cond);

    return 0;
}
